<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-6-1
 * Time: 19:29
 */

namespace backend\models\forms;


use common\models\game\star97\core\MachineRewardPoolStar97;
use common\models\game\star97\MachineListStar97;
use common\models\game\star97\TyMachinePrefabBigReward;
use common\services\Messenger;
use common\services\ToolService;
use yii\base\Model;

class Star97OddsForm extends Model
{
    public $all_fruits_base_count;
    public $mixed_bars_base_count;
    public $star_reward_appearance_base_count;
    public $double_time_base_count;
    public $three_time_base_count;
    public $four_time_base_count;
    public $play_add_buff_count;
    public $current_reward_type;
    public $current_buff_count;
    public $play_add_buff_count2;
    public $current_reward_type2;
    public $current_buff_count2;
    public $play_add_buff_count3;
    public $current_reward_type3;
    public $current_buff_count3;
    public $machine_model;
    public $jiangchi1_model;
    public $jiangchi2_model;
    public $jiangchi3_model;
    public $title;
    public $machine_arr_result;
    public $operate_prize;
    public $machine_arr = [];

    public function rules()
    {
        return [
            [['all_fruits_base_count','mixed_bars_base_count','star_reward_appearance_base_count','double_time_base_count','three_time_base_count','four_time_base_count','play_add_buff_count','play_add_buff_count2','play_add_buff_count3','current_reward_type','current_reward_type2','current_reward_type3'],'integer'],
            [['current_buff_count','current_buff_count2','current_buff_count3'],'integer'],
            [['machine_model','jiangchi1_model','jiangchi2_model','jiangchi3_model','title'],'safe'],
            [['double_time_base_count','three_time_base_count','four_time_base_count'],'validate_base_count'],
            [['all_fruits_base_count',],'number','min'=>1],
            [['mixed_bars_base_count',],'number','min'=>1],
            [['star_reward_appearance_base_count',],'number','min'=>1],
        ]; // TODO: Change the autogenerated stub
    }

    public function attributeLabels()
    {
        return [
            'all_fruits_base_count' => '全盘水果',
            'mixed_bars_base_count' => '全盘BAR',
            'star_reward_appearance_base_count' => '明星奖',
            'double_time_base_count' => '两倍',
            'three_time_base_count' => '三倍',
            'four_time_base_count' => '四倍',
            'play_add_buff_count' => '奖池1每局累积buff',
            'current_reward_type' => '奖池1出奖类型',
            'current_buff_count' => '奖池1累积buff值',
            'play_add_buff_count2' => '奖池2每局累积buff',
            'current_reward_type2' => '奖池2出奖类型',
            'current_buff_count2' => '奖池2累积buff值',
            'play_add_buff_count3' => '奖池3每局累积buff',
            'current_reward_type3' => '奖池3出奖类型',
            'current_buff_count3' => '奖池3累积buff值',
        ]; // TODO: Change the autogenerated stub
    }

    public function validate_base_count($attribute){
        $base_count = $this->double_time_base_count + $this->three_time_base_count + $this->four_time_base_count;

        if(  $base_count < 1 ){
            $this->addError($attribute,'两倍 三倍 四倍的和不能小于零!');
        }
    }



    public function init_model($id_arr,$level)
    {
        $this->machine_model = MachineListStar97::find()->filterWhere(['in', 'auto_id', $id_arr])->all();
        $this->jiangchi1_model = MachineRewardPoolStar97::find()->filterWhere(['like','seo_machine_id',$this->machine_model[0]->seo_machine_id])->andFilterWhere(['pool_id'=>1])->one();
        $this->jiangchi2_model = MachineRewardPoolStar97::find()->filterWhere(['like','seo_machine_id',$this->machine_model[0]->seo_machine_id])->andFilterWhere(['pool_id'=>2])->one();


        $this->all_fruits_base_count = $this->machine_model[0]->all_fruits_base_count;
        $this->mixed_bars_base_count = $this->machine_model[0]->mixed_bars_base_count;
        $this->star_reward_appearance_base_count = $this->machine_model[0]->star_reward_appearance_base_count;
        $this->double_time_base_count = $this->machine_model[0]->double_time_base_count;
        $this->three_time_base_count = $this->machine_model[0]->three_time_base_count;
        $this->four_time_base_count = $this->machine_model[0]->four_time_base_count;

        $this->play_add_buff_count = $this->jiangchi1_model->play_add_buff_count;
        $this->current_reward_type = $this->jiangchi1_model->current_reward_type;
        $this->current_buff_count = $this->jiangchi1_model->current_buff_count;
        $this->play_add_buff_count2 = $this->jiangchi2_model->play_add_buff_count;
        $this->current_reward_type2 = $this->jiangchi2_model->current_reward_type;
        $this->current_buff_count2 = $this->jiangchi2_model->current_buff_count;
        if($level == 1) {
            $this->jiangchi3_model = TyMachinePrefabBigReward::find()->filterWhere(['like','seo_machine_id',$this->machine_model[0]->seo_machine_id])->one(); // TODO: Change the autogenerated stub
            $this->play_add_buff_count3 = $this->jiangchi3_model->play_add_buff_count;
            $this->current_reward_type3 = $this->jiangchi3_model->current_reward_type;
            $this->current_buff_count3 = $this->jiangchi3_model->current_buff_count;
        }

        $this->machine_arr_result = [
            'all_fruits_base_count' => $this->all_fruits_base_count,
            'mixed_bars_base_count' => $this->mixed_bars_base_count,
            'star_reward_appearance_base_count' => $this->star_reward_appearance_base_count,
            'double_time_base_count' => $this->double_time_base_count,
            'three_time_base_count' => $this->three_time_base_count,
            'four_time_base_count' => $this->four_time_base_count,
            'play_add_buff_count' => $this->play_add_buff_count,
            'current_reward_type' => $this->current_reward_type,
            'current_buff_count' => $this->current_buff_count,
            'play_add_buff_count2' => $this->play_add_buff_count2,
            'current_reward_type2' => $this->current_reward_type2,
            'current_buff_count2' => $this->current_buff_count2,
        ];
        $this->machine_arr_result['play_add_buff_count3'] = $this->play_add_buff_count3;
        $this->machine_arr_result['current_reward_type3'] = $this->current_reward_type3;
        $this->machine_arr_result['current_buff_count3'] = $this->current_buff_count3;

        $title = null;
        foreach ($this->machine_model as $model) {
            $this->machine_arr[] = $model->toArray();
            if (empty($model->playerInfo)) {
                $title .= '，' . $model->seo_machine_id;
            } else {
                $title .= '，' . $model->seo_machine_id . '(' . ($model->playerInfo->nick_name) . ')';
            }
        }
        $this->title = mb_substr($title, 1);

    }

    public function init_data()
    {
        $this->all_fruits_base_count = null;
        $this->mixed_bars_base_count = null;
        $this->star_reward_appearance_base_count = null;
        $this->double_time_base_count = null;
        $this->three_time_base_count = null;
        $this->four_time_base_count = null;
        $this->play_add_buff_count = null;
        $this->current_reward_type = null;
        $this->current_buff_count = null;
        $this->play_add_buff_count2 = null;
        $this->current_reward_type2 = null;
        $this->current_buff_count2 = null;
        $this->play_add_buff_count3 = null;
        $this->current_reward_type3 = null;
        $this->current_buff_count3 = null;
    }


    public function updateMachine($model)
    {

        $arr['MachineListStar97'] = [
            'all_fruits_base_count' => $this->all_fruits_base_count,
            'mixed_bars_base_count' => $this->mixed_bars_base_count,
            'star_reward_appearance_base_count' => $this->star_reward_appearance_base_count,
            'double_time_base_count' => $this->double_time_base_count,
            'three_time_base_count' => $this->three_time_base_count,
            'four_time_base_count' => $this->four_time_base_count,
        ];

        foreach($arr['MachineListStar97'] as $key => $val){
            if($val == ''){
                unset($arr['MachineListStar97'][$key]);
            }
        }
        //$arr['MachineListStar97'] = array_filter($arr['MachineListStar97']);
        $message = new Messenger();
        if($model -> load($arr) && $model->validate() && $model->save()){

        }else{
            $message -> status = 0;
            $error_str = null;
            foreach ($model->errors as $error){
                $error_str .= $error[0].',';
            }
            $message ->errors = $error_str;
        }

        return $message;
    }

    public function updateJiangchi1($jiangchi1)
    {
        $arr['MachineRewardPoolStar97'] = [
            'play_add_buff_count' => $this->play_add_buff_count,
            'current_reward_type' => $this->current_reward_type,
            'current_buff_count' => $this->current_buff_count,
        ];
        foreach($arr['MachineRewardPoolStar97'] as $key => $val){
            if($val == ''){
                unset($arr['MachineRewardPoolStar97'][$key]);
            }
        }

        $message = new Messenger();
        if($jiangchi1 -> load($arr) && $jiangchi1->validate() && $jiangchi1->save()){

        }else{
            $message -> status = 0;
            $error_str = null;
            foreach ($jiangchi1->errors as $error){
                $error_str .= $error[0].',';
            }
            $message ->errors = $error_str;
        }

        return $message;
    }

    public function updateJiangchi2($jiangchi2)
    {
        $arr['MachineRewardPoolStar97'] = [
            'play_add_buff_count' => $this->play_add_buff_count2,
            'current_reward_type' => $this->current_reward_type2,
            'current_buff_count' => $this->current_buff_count2,
        ];
        foreach($arr['MachineRewardPoolStar97'] as $key => $val){
            if($val == ''){
                unset($arr['MachineRewardPoolStar97'][$key]);
            }
        }

        $message = new Messenger();
        if($jiangchi2 -> load($arr) && $jiangchi2->validate() && $jiangchi2->save()){

        }else{
            $message -> status = 0;
            $error_str = null;
            foreach ($jiangchi2->errors as $error){
                $error_str .= $error[0].',';
            }
            $message ->errors = $error_str;
        }

        return $message;
    }

    public function updateJiangchi3($jiangchi3)
    {
        $arr['TyMachinePrefabBigReward'] = [
            'play_add_buff_count' => $this->play_add_buff_count3,
            'current_reward_type' => $this->current_reward_type3,
            'current_buff_count' => $this->current_buff_count3,
        ];
        foreach($arr['TyMachinePrefabBigReward'] as $key => $val){
            if($val == ''){
                unset($arr['TyMachinePrefabBigReward'][$key]);
            }
        }

        $message = new Messenger();
        if($jiangchi3 -> load($arr) && $jiangchi3->validate() && $jiangchi3->save()){

        }else{
            $message -> status = 0;
            $error_str = null;
            foreach ($jiangchi3->errors as $error){
                $error_str .= $error[0].',';
            }
            $message ->errors = $error_str;
        }

        return $message;
    }
}